//
//  XLUserDataHeaderView.m
//  XiangLove
//
//  Created by 宋振 on 2018/7/25.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLUserDataHeaderView.h"

#import "XLUserInfoDetailModel.h"

#import "UIImage+WebP.h"


@interface XLUserDataCollectionCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView *giftView;

@end

@implementation XLUserDataCollectionCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.giftView = [[UIImageView alloc] init];
        [self addSubview:self.giftView];
        [self.giftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        self.giftView.layer.cornerRadius = 20;
        self.giftView.layer.masksToBounds = YES;
        
    }
    return self;
}
@end




@interface XLUserDataHeaderView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *  collectionView;

@end

@implementation XLUserDataHeaderView

- (instancetype)init
{
    self = [super init];
    
    if (self) {
        [self setCustomeView];
    }
    
    return self;
}

#pragma mark    - - - 设置界面显示 - - -

- (void)setCustomeView
{
    self.backgroundColor = XLThemeColor;
    
    [self layOutCollection];
}

//绘制容器
- (void)layOutCollection
{
    //礼物列表
    UILabel *giftList = [[UILabel alloc] init];
    
    [self addSubview:giftList];
    
    giftList.font = [UIFont systemFontOfSize:14];
    giftList.textColor = RGB(144, 144, 144);
    giftList.text = @"礼物列表";
    
    [giftList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(self.mas_top).offset(13);
    }];
    
    
    //显示列表
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    flowLayout.itemSize = CGSizeMake(40, 40);
    flowLayout.minimumInteritemSpacing = 12;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.userInteractionEnabled = NO;
    
    self.collectionView.backgroundColor = [UIColor clearColor];
    
    [self addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(45, 15, 25, 30));
    }];
    
    [self.collectionView registerClass:[XLUserDataCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([XLUserDataCollectionCell class])];
    
    //指向
    UIImageView *arrowImageView = [[UIImageView alloc] init];
    
    arrowImageView.image = [[UIImage imageNamed:@"xl_arrow_icon"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];
    
    [self addSubview:arrowImageView];
    
    [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(17, 16));
        make.centerY.equalTo(_collectionView.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-10);
    }];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count > 5 ? 5 : self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XLUserDataCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XLUserDataCollectionCell class]) forIndexPath:indexPath];
    
//    GiftsModel *model = self.dataArray[indexPath.row];
    
    NSDictionary *dic = self.dataArray[indexPath.row];
    
    NSString *url = dic[@"Url"];
    
    NSError *error;
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];

    UIImage * image = [UIImage sd_imageWithWebPData:data];
    cell.giftView.image = image;
    
    return cell;
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CGPoint point = [[touches anyObject] locationInView:self];

    if ([self.delegate respondsToSelector:@selector(userDataHeaderViewDidSelect)]) {
        [self.delegate userDataHeaderViewDidSelect];
    }

}

-(BOOL)prefersStatusBarHidden{
    return NO;
}


@end
